package com.netafull.twitter;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class TwitterConnect {
	String consumerkey = "OFHLCYfVTISPV2SBQNG2A";
	String consumersecret = "czfALOAsT6wWdebxvyYaWO4b3GG5MFqVuDj4HuyqZs";
	String tokenstr = "TwitBankcachedXAuthAccessTokenKey";
	public static AccessToken accessToken = null;
	public static Twitter twitter;
	
	public static RequestToken requestToken = null;
    private int REQUEST_CODE = 10;
    private String pin;
    Context con;
    public TwitterConnect(Context _con){
    	this.con = _con;
    }
    //oauth
	public String twitterLoginOauth() throws TwitterException{
		String re = "";
        if(twitter != null) twitter.shutdown();
		twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONST.CONSUMER_KEY, CONST.CONSUMER_SECRET);
        requestToken = twitter.getOAuthRequestToken();
//        re = requestToken.getAuthorizationURL();
        re = requestToken.getAuthenticationURL()+"&force_login=true";
        
        return re;
	}
	//xauth
	public int twitterLogin(String username,String password){
		int  isOK = 2;
        if(twitter != null) twitter.shutdown();
		twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(consumerkey, consumersecret);
		int i = 0;
		while (null == accessToken) {
			i++;
			try{
				accessToken = twitter.getOAuthAccessToken("","");
				if(accessToken != null){
					isOK = 1;
				}else{
				}
			} catch (TwitterException te) {
				if(401 == te.getStatusCode()){
					return isOK;
				}else{
					te.printStackTrace();
					return isOK;
				}
			}

			if(i >= 10){
				break;
			}
		}
		
		return isOK;
	}
	/**
	 * 获取 登陆用户 头像跟名字
	 * @return
	 */
	public HashMap<String, Object> getUserInfo(){
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			Bitmap imgbit = BitmapFactory.decodeStream(twitter.showUser(accessToken.getUserId()).getProfileImageURL().openStream());
			map.put("userimg", imgbit);
			map.put("username",twitter.showUser(accessToken.getUserId()).getName());
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 取登陆用户名
	 * @return
	 */
	public String getUserName(){
		try {
			return twitter.showUser(accessToken.getUserId()).getName();
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 获取头像
	 * @return
	 */
	public Bitmap getUserImg(String url){
		Bitmap imgbit = null;
		try {
			if(url != null){
				URL imgurl = new URL(url);
//				imgbit =  BitmapFactory.decodeStream(twitter.showUser(accessToken.getUserId()).getProfileImageURL().openStream());
				imgbit = BitmapFactory.decodeStream(imgurl.openStream());
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imgbit;
	}
	/**
	 * 投稿
	 * @param text
	 * @param bm
	 * @return
	 */
	public boolean TwitterContribute(String text) {
		boolean isok = false;
		Status status = null;
		try {
			
			Log.e("caget", "TwitterContribute");
			Log.e("caget", "TwitterContribute text = " + text);
			status = twitter.updateStatus(text);
//			status = twitter.updateStatus("22222223");
			Log.e("caget", "updateStatus status = " + status);
			if (status != null) {
				Log.e("caget", "status = " + status.getText());
				System.out.println(status.getText());
				isok = true;
			}
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			Log.e("caget", "TwitterContribute exception status = " + status);
			e.printStackTrace();
			isok = false;
		}

		return isok;
	}
}
